<template>
    <div class="users_container">
      <!-- 面包屑导航区域 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 卡片视图区域 -->
      <el-card>
        <!-- 搜索与添加区域 -->
        
        <div class="user_s">找回密码</div>
        <hr class="hr">
        <!-- 用户列表数据 -->
        <!-- 进度条 -->
        <el-steps :active="active" finish-status="success">
          <el-step title="填写用户名"></el-step>
          <el-step title="验证身份"></el-step>
          <el-step itle="设置新密码"></el-step>
          <el-step titlte="完成"></el-step>
        </el-steps>
  
        <!-- 验证用户名start -->
       <div class="step_4">新密码设置成功</div>
       <div class="step_4_1">请牢记你的新密码</div>

        <!-- 验证用户名end -->
        
        <el-button class="el-form-f1 " style="margin-top: 82px;" @click="next">返回首页</el-button>
      
      </el-card>
  
      <!-- 添加用户的对话框 -->
      <el-dialog title="添加用户" @close="addDialogClosed" :visible.sync="addDialogVisible" width="50%">
        <!-- 内容主体区域 -->
        <!-- 这是主题区域 -->
        <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="addForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="addForm.password"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="addForm.email"></el-input>
          </el-form-item>
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="addForm.mobile"></el-input>
          </el-form-item>
        </el-form>
        <!-- 底部区域 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible=false">取 消</el-button>
          <el-button type="primary" @click="addUser">确 定</el-button>
        </span>
      </el-dialog>
  
      <!-- 修改用户的对话框 -->
      <el-dialog title="修改用户" :visible.sync="editDialogVisible" width="50%">
        <!-- 内容主体区域 -->
        <!-- 这是修改用户区域 -->
        <!-- 表单的引用以Ref结尾 -->
        <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
          <el-form-item label="用户名">
            <el-input v-model="editForm.username" disabled></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="editForm.email"></el-input>
          </el-form-item>
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="editForm.mobile"></el-input>
          </el-form-item>
        </el-form>
        <!-- 底部区域 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editUserInfo">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { async } from 'q'
  
  export default {
    data() {
      var checkEmail = (rule, value, cb) => {
        // 验证邮箱的正则表达式
        const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
        if (regEmail.test(value)) {
          // 合法的邮箱
          return cb()
        }
        cb(new Error('请输入合法的邮箱'))
      }
      // 验证手机号的规则
      var checkMobile = (rule, value, cb) => {
        // 验证手机号的正则表达式
        const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
        if (regMobile.test(value)) {
          return cb()
        }
        cb(new Error('请输入合法的手机号'))
      }
      return {
        form: {
            name: '',
            region: '',
            date1: '',
            date2: '',
            delivery: false,
            type: [],
            resource: '',
            desc: ''
          },
        active:4,
        editDialogVisible: false,
        addDialogVisible: false,
        editForm: {},
        // 修改表单的验证规则对象
        total: 0,
        userlist: [],
        queryInfo: {
          query: '',
          pagenum: 1,
          pagesize: 5
        },
        // 添加用户的表单数据
        addForm: {
          username: '',
          password: '',
          email: '',
          mobile: ''
        },
  
        // 这是个编辑表单的验证规则
        editFormRules: {
          email: [
            { required: true, message: '请输入邮箱', trigger: 'blur' },
            { validator: checkEmail, trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            { validator: checkMobile, trigger: 'blur' }
          ]
        },
        addFormRules: {
          // 添加表单的验证规则对象
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            {
              min: 3,
              max: 10,
              message: '用户名的长度在3~10个字符之间',
              trigger: 'blur'
            }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            {
              min: 6,
              max: 15,
              message: '用户名的长度在6~15个字符之间',
              trigger: 'blur'
            }
          ],
          email: [
            { required: true, message: '请输入邮箱', trigger: 'blur' },
            { validator: checkEmail, trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            { validator: checkMobile, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      next() {
          if (this.active++ > 2) this.active = 0;
          this.$router.push('/users')
        },
      // axios 发起网络请求数据
      async getUserList() {
        const { data: res } = await this.$http.get('users', {
          params: this.queryInfo
        })
        // console.log(res)
        if (res.meta.status !== 200) {
          return this.$message.console.error('获取用户列表失败')
        }
        // console.log(res)
        this.userlist = res.data.users
        this.total = res.data.total
        // console.log(res, 222)
        // console.log(res.data.users)
      },
      handleSizeChange(newSize) {
        // console.log(`每页 ${val} 条`)
        this.queryInfo.pagesize = newSize
        this.getUserList()
        console.log(this.queryInfo)
      },
      handleCurrentChange(newPage) {
        // console.log(`当前页: ${val}`)
        this.queryInfo.pagenum = newPage
        this.getUserList()
      },
      async userStateChange(userinfo) {
        console.log(userinfo)
        const { data: res } = await this.$http.put(
          `users/${userinfo.id}/state/${userinfo.mg_state}`
        )
        if (res.meta.status !== 200) {
          userinfo.mg_state = !userinfo.mg_state
          return this.$message.error('更新失败')
        }
        this.$message.success('更新成功')
      },
      //关闭清空文本框
      addDialogClosed() {
        this.$refs.addFormRef.resetFields()
      },
      addUser() {
        this.$refs.addFormRef.validate(async valid => {
          if (!valid) return //判断是否符合验证
          const { data: res } = await this.$http.post('users', this.addForm)
          // console.log(res)
          if (res.meta.status !== 200) {
            this.$message.error('添加用户失败')
          }
          this.$message.success('添加用户成功')
          // 隐藏添加用户的对话框
          this.addDialogVisible = false
          // 重新获取用户列表数据
          this.getUserList()
        })
      },
      // 删除用户信息
      async removeUserById(id) {
        // 根据Id删除对应的用户信息
        // 弹框询问用户是否删除数据
        const confirmResult = await this.$confirm(
          '此操作将永久删除该用户, 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).catch(err => err)
        // 如果用户确认删除，则返回值为字符串 confirm
        // 如果用户取消了删除，则返回值为字符串 cancel
        // console.log(confirmResult)
        if (confirmResult !== 'confirm') {
          return this.$message.info('已取消删除')
        }
        const { data: res } = await this.$http.delete('users/' + id)
        if (res.meta.status !== 200) {
          return this.$message.error('删除用户失败！')
        }
        this.$message.success('删除用户成功！')
        this.getUserList()
      },
      //修改用户数据的函数
      async showEditDialog(id) {
        // console.log(id)
  
        const { data: res } = await this.$http.get('users/' + id)
        this.editForm = res.data
        if (res.meta.status !== 200) {
          return this.$message.error('修改用户失败')
        }
        this.$message.success('修改用户成功')
        // console.log(res);
        this.editDialogVisible = true
      },
      editUserInfo() {
        this.$refs.editFormRef.validate(async valid => {
          console.log(valid)
          if (!valid) return
          console.log(this.editForm)
          const { data: res } = await this.$http.put(
            'users/' + this.editForm.id,
            {
              email: this.editForm.email,
              mobile: this.editForm.mobile
            }
          )
          console.log(res)
        })
        this.getUserList()
        this.editDialogVisible = false
      }
    },
    created() {
      this.getUserList()
    }
  }
  </script>>
  <style scoped>
    .step_1{
      width: 200px;
    }
    .user_s{
line-height: 50px;
height: 50px;
font-size: 14px;
}
.hr{
color: blue;
border: 1px solid #eee;
margin-left: -20px;
margin-bottom: 25px;
}

.el-form-f{
margin:50px 0 0 230px;
}
.el-form-f1{
margin:50px 0 0 425px;
background-color: #4bd083;

}
.step_4{
    width: 150px;
    font-size: 13px;
    color: #75C560;
    margin:10px auto;
    font-weight: bold;
}
.step_4_1{
    width: 150px;
    font-size: 13px;
    margin: auto;
}
  </style>